/* Copyright (c) 2011 by Sana, All Rights Reserved
 Contact Author Ikaro (ikaro@mit.edu)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307  USA
 */
package org.moca.activity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.framingham.risk.Afib10yr;
import org.framingham.risk.Chd10yr;
import org.framingham.risk.Diabetes;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.net.Uri;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;


public class FunctionEval extends Activity {
	Uri mData;
	public static final String FUNCEVAL_CONTENT_TYPE = "application/functioneval";
	private TextView mStatus;
	private Button detail, exit;
	Map<String, String> data = new HashMap<String, String>();
	public static final String TAG = FunctionEval.class.toString();
	
	private View createView() {
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);

		LinearLayout hl = new LinearLayout(this);
		hl.setOrientation(LinearLayout.HORIZONTAL);


		mStatus = new TextView(this);
		mStatus.setPadding(5, 0, 5, 0);
		String title=data.get("procedure");
		String result=null;
		Log.i(TAG,"Getting risk for: " + title);
		if(title.contentEquals("Coronary Heart Disease (10 yr)")){
			//Caculate and Display the RISK -- very basic right now
			Chd10yr myRisk= new Chd10yr();
			myRisk.calcRisk(data);
			Log.i(TAG,"Done calculating risk for: " + title);
			result=myRisk.print_risk();
			Log.i(TAG,"Done getting result for: " + title);
		}
		else if (title.contentEquals("Atrial Fibrillation Risk (10 yr)")){
			Afib10yr myRisk= new Afib10yr();
			myRisk.calcRisk(data);
			Log.i(TAG,"Done calculating risk for: " + title);
			result=myRisk.print_risk();
			Log.i(TAG,"Done getting result for: " + title);
		}
		else if (title.contentEquals("Diabetes Risk Score (8 yrs)")){
			Diabetes myRisk= new Diabetes();
			myRisk.calcRisk(data);
			Log.i(TAG,"Done calculating risk for: " + title);
			result=myRisk.print_risk();
			Log.i(TAG,"Done getting result for: " + title);
		}
		else{
			result="Risk score not found!!";
		}
		

		//ARIVIND TO DO: Implement CvdFram Here
		//Might be good to have the folowing signature (note data is a map!!)
		//CvdFram myCvdFram= new CvdFram();
		//myCvdFram.getRisks(data);

		mStatus.setTextSize(20);
		mStatus.setTextColor(Color.GRAY);
		//ARVIND TO DO: can display value from myCvdFram here
		setStatus(result);
		detail = new Button(this);
		detail.setEnabled(true);               
		detail.setText("Detailed Analysis");
		detail.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				setStatus("Generating detailed analysis...");
			}
		});
		detail.setPadding(5, 0, 5, 0);

		exit = new Button(this);
		exit.setEnabled(true);
		exit.setText("Exit");
		exit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		exit.setPadding(5, 0, 5, 0);


		//hl.addView(start);
		hl.addView(detail);
		hl.addView(exit);

		ll.addView(hl);
		ll.addView(mStatus);


		LinearLayout topRow = new LinearLayout(this);
		topRow.setOrientation(LinearLayout.HORIZONTAL);
		LinearLayout middleRow = new LinearLayout(this);
		middleRow.setOrientation(LinearLayout.HORIZONTAL);
		LinearLayout bottomRow = new LinearLayout(this);
		bottomRow.setOrientation(LinearLayout.HORIZONTAL);


		ScrollView scrollView = new ScrollView(this);
		scrollView.addView(ll);
		return scrollView;
	}

	private void setStatus(String status) {
		if (mStatus != null)
			mStatus.setText(status);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//Get answers from questionaire 
		Bundle answers = getIntent().getExtras();
		Set keys= answers.keySet();
		Iterator it=keys.iterator();
		while (it.hasNext()) {
			String tmp_ky=(String) it.next();
			data.put(tmp_ky,answers.getString(tmp_ky));
		}

		setContentView(createView());
		if(this.getIntent().hasExtra("patient_file_name")){
			mData = getIntent().getData();
			//pt_file_name= this.getIntent().getExtras().getString("patient_file_name");
		}else{
			//pt_file_name = "test.txt";
		}
	}

	Intent mIntent=null;
	@Override
	public void onStart() {
		super.onStart();

	}

	@Override
	public void onDestroy(){
		super.onDestroy();
	}

}
